package com.heavenbird.book.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.heavenbird.book.BookConst;
import com.heavenbird.book.dao.interfaces.IUserCategoryDefDAO;
import com.heavenbird.book.entity.UserCategoryDef;
import com.heavenbird.common.util.DBUtil;

public class UserCategoryDefDAOImpl implements IUserCategoryDefDAO {

	public void newUserCategoryDef(UserCategoryDef userCategoryDef) throws Exception {
		Session session = DBUtil.getSession();
		session.save(userCategoryDef);
	}
	
	public UserCategoryDef[] getCategory(long parentId, int sts) throws Exception {
		Session session = DBUtil.getSession();
		Criteria criteria = session.createCriteria(UserCategoryDef.class);
		criteria.add(Restrictions.eq("parentId", parentId));
		if(sts != BookConst.USERCATEGORYDEF.STS_0) {
			criteria.add(Restrictions.eq("sts", sts));
		}
		criteria.addOrder(Order.asc("sortId"));
		List list = criteria.list();
		return (UserCategoryDef[]) list.toArray(new UserCategoryDef[0]);
	}
	
	public UserCategoryDef[] getCategory(long parentId, int categoryType, int sts) throws Exception {
		Session session = DBUtil.getSession();
		Criteria criteria = session.createCriteria(UserCategoryDef.class);
		criteria.add(Restrictions.eq("parentId", parentId));
		criteria.add(Restrictions.eq("categoryType", categoryType));
		if(sts != BookConst.USERCATEGORYDEF.STS_0) {
			criteria.add(Restrictions.eq("sts", sts));
		}
		criteria.addOrder(Order.asc("sortId"));
		List list = criteria.list();
		return (UserCategoryDef[]) list.toArray(new UserCategoryDef[0]);
	}
	
	public UserCategoryDef[] getCanModifyCategory(long userId, long parentId, int sts) throws Exception {
		Session session = DBUtil.getSession();
		Criteria criteria = session.createCriteria(UserCategoryDef.class);
		criteria.add(Restrictions.eq("userId", userId));
		criteria.add(Restrictions.eq("parentId", parentId));
		if(sts != BookConst.USERCATEGORYDEF.STS_0) {
			criteria.add(Restrictions.eq("sts", sts));
		}
		criteria.addOrder(Order.asc("categoryType"));
		criteria.addOrder(Order.asc("sortId"));
		List list = criteria.list();
		return (UserCategoryDef[]) list.toArray(new UserCategoryDef[0]);
	}
	
	public UserCategoryDef[] getCategory(long userId, long parentId, int categoryType, int sts) throws Exception {
		Session session = DBUtil.getSession();
		Criteria criteria = session.createCriteria(UserCategoryDef.class);
		criteria.add(Restrictions.in("userId", new Long[]{userId, BookConst.USERCATEGORYDEF.CATEGORY_DEF_DEFAULT_USER_ID}));
		criteria.add(Restrictions.eq("parentId", parentId));
		criteria.add(Restrictions.eq("categoryType", categoryType));
		if(sts != BookConst.USERCATEGORYDEF.STS_0) {
			criteria.add(Restrictions.eq("sts", sts));
		}
		criteria.addOrder(Order.asc("sortId"));
		List list = criteria.list();
		return (UserCategoryDef[]) list.toArray(new UserCategoryDef[0]);
	}
	
	public UserCategoryDef[] getCategory(long userId, long parentId, int sts) throws Exception {
		Session session = DBUtil.getSession();
		Criteria criteria = session.createCriteria(UserCategoryDef.class);
		criteria.add(Restrictions.in("userId", new Long[]{userId, BookConst.USERCATEGORYDEF.CATEGORY_DEF_DEFAULT_USER_ID}));
		criteria.add(Restrictions.eq("parentId", parentId));
		if(sts != BookConst.USERCATEGORYDEF.STS_0) {
			criteria.add(Restrictions.eq("sts", sts));
		}
		//criteria.addOrder(Order.asc("categoryType"));
		criteria.addOrder(Order.asc("sortId"));
		List list = criteria.list();
		return (UserCategoryDef[]) list.toArray(new UserCategoryDef[0]);
	}
	
	public UserCategoryDef getCategoryInfo(long categoryId, int sts) throws Exception {
		Session session = DBUtil.getSession();
		Criteria criteria = session.createCriteria(UserCategoryDef.class);
		criteria.add(Restrictions.eq("categoryId", categoryId));
		if(sts != BookConst.USERCATEGORYDEF.STS_0) {
			criteria.add(Restrictions.eq("sts", sts));
		}
		//criteria.addOrder(Order.asc("categoryType"));
		criteria.addOrder(Order.asc("sortId"));
		List list = criteria.list();
		if (list.size() > 0) {
			return (UserCategoryDef) list.get(0);
		} else {
			return null;
		}
	}
	
	public UserCategoryDef[] getCategoryByType(long userId, int categoryType, int sts) throws Exception {
		Session session = DBUtil.getSession();
		Criteria criteria = session.createCriteria(UserCategoryDef.class);
		criteria.add(Restrictions.eq("userId", userId));
		criteria.add(Restrictions.eq("categoryType", categoryType));
		criteria.add(Restrictions.eq("parentId", BookConst.USERCATEGORYDEF.CATEGORY_ID_ROOT));
		if(sts != BookConst.USERCATEGORYDEF.STS_0) {
			criteria.add(Restrictions.eq("sts", sts));
		}
		criteria.addOrder(Order.asc("sortId"));
		List list = criteria.list();
		return (UserCategoryDef[]) list.toArray(new UserCategoryDef[0]);
	}
	
	public UserCategoryDef[] getCategoryList(long userId, int sts) throws Exception {
		Session session = DBUtil.getSession();
		Criteria criteria = session.createCriteria(UserCategoryDef.class);
		criteria.add(Restrictions.in("userId", new Long[]{userId, BookConst.USERCATEGORYDEF.CATEGORY_DEF_DEFAULT_USER_ID}));
		if(sts != BookConst.USERCATEGORYDEF.STS_0) {
			criteria.add(Restrictions.eq("sts", sts));
		}
		criteria.addOrder(Order.asc("categoryType"));
		criteria.addOrder(Order.asc("parentId"));
		criteria.addOrder(Order.asc("sortId"));
		List list = criteria.list();
		return (UserCategoryDef[]) list.toArray(new UserCategoryDef[0]);
	}
	
	public void updateCategoryInfo(UserCategoryDef userCategoryDef) throws Exception {
		Session session = DBUtil.getSession();
		session.update(userCategoryDef);
	}
}
